package gui;

import java.awt.EventQueue;

import javax.security.auth.Subject;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Pattern;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFormattedTextField;

import dao.SubjectDao;
import dto.AdminDto;
import dto.StudentDto;
import dto.SubjectDto;
import dto.TeacherDto;
import dto.UserDto;
import bus.CreateUserBus;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class JFrameAdminCreateNewUser extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsername;
	private JTextField txtFullName;
	private JTextField txtEmail;
	private JTextField txtPass;
	private JFormattedTextField txtDob;
	private JComboBox cboRole;
	private JComboBox cboSubject01;
	private JComboBox cboSubject02;
	private JComboBox cboSubject03;
	private JComboBox cboSubject04;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameAdminCreateNewUser frame = new JFrameAdminCreateNewUser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameAdminCreateNewUser() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent arg0) {
				cboSubject01.addItem("Chose subject");
				cboSubject02.addItem("Chose subject");
				cboSubject03.addItem("Chose subject");
				cboSubject04.addItem("Chose subject");

				SubjectDao subjectDao = new SubjectDao();
				ArrayList<SubjectDto> allSubject = subjectDao.getAllSubject();
				for (SubjectDto subjectDto : allSubject) {
					cboSubject01.addItem(subjectDto.getSubjectName());
					cboSubject02.addItem(subjectDto.getSubjectName());
					cboSubject03.addItem(subjectDto.getSubjectName());
					cboSubject04.addItem(subjectDto.getSubjectName());
				}
			}

			@Override
			public void windowClosing(WindowEvent e) {
				JFrameAdminMainPage jFrameAdminMainPage = new JFrameAdminMainPage();
				jFrameAdminMainPage.setVisible(true);
			}
		});
		setResizable(false);
		setTitle("Admin - Create new user");
		setBounds(100, 100, 468, 435);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblEnterNewUser = new JLabel("Enter new user's detail:");
		lblEnterNewUser.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEnterNewUser.setBounds(107, 48, 278, 14);
		contentPane.add(lblEnterNewUser);

		JLabel lblNewLabel = new JLabel("User ID:");
		lblNewLabel.setBounds(107, 80, 90, 14);
		contentPane.add(lblNewLabel);

		txtUsername = new JTextField();
		txtUsername.setBounds(197, 77, 137, 20);
		contentPane.add(txtUsername);
		txtUsername.setColumns(10);

		JLabel lblFullName = new JLabel("Full name:");
		lblFullName.setBounds(107, 108, 90, 14);
		contentPane.add(lblFullName);

		txtFullName = new JTextField();
		txtFullName.setColumns(10);
		txtFullName.setBounds(197, 105, 137, 20);
		contentPane.add(txtFullName);

		JLabel lblEmailAddress = new JLabel("Email address:");
		lblEmailAddress.setBounds(107, 136, 90, 14);
		contentPane.add(lblEmailAddress);

		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(197, 133, 137, 20);
		contentPane.add(txtEmail);

		JLabel lblDateOfBirth = new JLabel("Date of birth:");
		lblDateOfBirth.setBounds(107, 164, 90, 14);
		contentPane.add(lblDateOfBirth);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(107, 192, 90, 14);
		contentPane.add(lblPassword);

		txtPass = new JTextField();
		txtPass.setColumns(10);
		txtPass.setBounds(197, 189, 137, 20);
		contentPane.add(txtPass);

		JLabel lblRole = new JLabel("Role:");
		lblRole.setBounds(107, 220, 90, 14);
		contentPane.add(lblRole);

		cboRole = new JComboBox();
		cboRole.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				switch (((String) cboRole.getSelectedItem())) {
				case "Student":
					cboSubject01.setEnabled(true);
					cboSubject02.setEnabled(true);
					cboSubject03.setEnabled(true);
					cboSubject04.setEnabled(true);
					break;
				case "Teacher":
					cboSubject01.setEnabled(true);
					cboSubject02.setEnabled(true);
					cboSubject03.setEnabled(true);
					cboSubject04.setEnabled(true);
					break;
				case "Admin":
					cboSubject01.setEnabled(false);
					cboSubject02.setEnabled(false);
					cboSubject03.setEnabled(false);
					cboSubject04.setEnabled(false);
					break;
				default:
					break;
				}

			}
		});
		cboRole.setModel(new DefaultComboBoxModel(new String[] { "Student",
				"Teacher", "Admin" }));
		cboRole.setBounds(197, 217, 137, 20);
		contentPane.add(cboRole);

		JButton btnCreate = new JButton("Create");
		btnCreate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				if (checkDataInput()) {
					String username = txtUsername.getText();
					String fullName = txtFullName.getText();
					String email = txtEmail.getText();
					Date dob = new Date(txtDob.getText());
					String pass = txtPass.getText();
					String role = (String) cboRole.getSelectedItem();

					CreateUserBus createUserBus = new CreateUserBus();
					UserDto user = new UserDto(-1, fullName, email, dob, role);
					String respone = "";

					switch (role) {
					case "Student":

						StudentDto newStudent = new StudentDto(username, pass,
								-1);
						respone = createUserBus.createNewStudent(newStudent,
								user, getListSubject());
						JOptionPane.showMessageDialog(
								JFrameAdminCreateNewUser.this, respone);
						break;
					case "Teacher":

						TeacherDto newTearcher = new TeacherDto(username, pass,
								-1);
						respone = createUserBus.createNewTeacher(newTearcher,
								user);
						JOptionPane.showMessageDialog(
								JFrameAdminCreateNewUser.this, respone);
						break;

					case "Admin":

						AdminDto newAdmin = new AdminDto(username, pass, -1);
						respone = createUserBus.createNewAdmin(newAdmin, user);
						JOptionPane.showMessageDialog(
								JFrameAdminCreateNewUser.this, respone);
						break;
					default:
						break;
					}

				}

			}
		});
		btnCreate.setBounds(245, 372, 89, 23);
		contentPane.add(btnCreate);

		txtDob = new JFormattedTextField(new SimpleDateFormat("MM/dd/yyyy"));
		txtDob.setBounds(197, 161, 137, 20);
		contentPane.add(txtDob);

		JLabel lblSubject = new JLabel("Subject ID1:");
		lblSubject.setToolTipText("");
		lblSubject.setBounds(107, 248, 90, 14);
		contentPane.add(lblSubject);

		cboSubject01 = new JComboBox();
		cboSubject01.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if (cboSubject01.getSelectedIndex() > 0)
					cboSubject02.setEnabled(true);
				else
					cboSubject02.setEnabled(false);
				cboSubject03.setEnabled(false);
				cboSubject04.setEnabled(false);
			}
		});
		cboSubject01.setBounds(197, 245, 137, 20);
		contentPane.add(cboSubject01);

		JLabel lblSubjectId = new JLabel("Subject ID2:");
		lblSubjectId.setBounds(107, 276, 90, 14);
		contentPane.add(lblSubjectId);

		cboSubject02 = new JComboBox();
		cboSubject02.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (cboSubject02.getSelectedIndex() > 0)
					cboSubject03.setEnabled(true);

				else {
					cboSubject03.setEnabled(false);
					cboSubject04.setEnabled(false);
				}
			}
		});
		cboSubject02.setEnabled(false);
		cboSubject02.setBounds(197, 273, 137, 20);
		contentPane.add(cboSubject02);

		JLabel lblSubjectId_1 = new JLabel("Subject ID3:");
		lblSubjectId_1.setBounds(107, 304, 90, 14);
		contentPane.add(lblSubjectId_1);

		cboSubject03 = new JComboBox();
		cboSubject03.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (cboSubject03.getSelectedIndex() > 0)
					cboSubject04.setEnabled(true);
				else
					cboSubject04.setEnabled(false);
			}
		});
		cboSubject03.setEnabled(false);
		cboSubject03.setBounds(197, 301, 137, 20);
		contentPane.add(cboSubject03);

		JLabel lblSubjectId_2 = new JLabel("Subject ID4:");
		lblSubjectId_2.setBounds(107, 332, 90, 14);
		contentPane.add(lblSubjectId_2);

		cboSubject04 = new JComboBox();
		cboSubject04.setEnabled(false);
		cboSubject04.setBounds(197, 329, 137, 20);
		contentPane.add(cboSubject04);
	}

	private void showMessage(String mess) {
		JOptionPane.showMessageDialog(this, mess);
	}

	private ArrayList<String> getListSubject() {
		ArrayList<String> arrResult = new ArrayList<>();
		if (cboSubject01.getSelectedIndex() > 0)
			arrResult.add((String) cboSubject01.getSelectedItem());
		if (cboSubject02.getSelectedIndex() > 0)
			arrResult.add((String) cboSubject02.getSelectedItem());
		if (cboSubject03.getSelectedIndex() > 0)
			arrResult.add((String) cboSubject03.getSelectedItem());
		if (cboSubject04.getSelectedIndex() > 0)
			arrResult.add((String) cboSubject04.getSelectedItem());

		return arrResult;
	}

	private boolean checkDataInput() {

		if (txtUsername.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter username!");
			return false;
		}
		if (txtFullName.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter full name!");
			return false;
		}
		if (txtEmail.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter email address!");
			return false;
		}

		final Pattern rfc2822 = Pattern
				.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");

		if (!rfc2822.matcher(txtEmail.getText()).matches()) {
			JOptionPane.showMessageDialog(this, "Invalid email address!");
			return false;
		}
		if (txtDob.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter date of birth!");
			return false;
		}
		if (txtPass.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Please enter password!");
			return false;
		}

		return true;
	}
}
